
//寒假写的，要改面向对象太麻烦，所以将个就，其他页面均采用面向对象实现

window.onload=function(){
	//搜索框
const shuru = $("input.in1");
//console.log(shuru);
shuru.keyup(function(){
	var v=shuru.val();
	const url = `https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=${v}&cb=?`;
$.getJSON(url, (res) => {
				console.log(res);
				var arr=res.s;
				var html="";
				arr.forEach(function(ele,index){
					html+=`<div>${ele}</div>`;
				});
				$(".jsonp").html(html);
});
});
$(".jsonp").click(function(event){
	var v=$(event.target).text();
	shuru.val(v);
});


$(document).click(function(){
	$(".jsonp").empty();
})








			// 跨域请求 baidu 的 jsonp 提示接口


	
var yd=$(".h1y")[0];
var start=-33;
var time1=setInterval(function(){
	animate(yd,{top:start}, 500,function(){
		start=start-33;
		if(start==-165)
		{
			yd.style.top=0+"px";
			start=-33;
		}
	});
},2000);
yd.onmouseover=function(){
	clearInterval(time1);
};
yd.onmouseleave=function(){
	time1=setInterval(function(){
	animate(yd,{top:start}, 500,function(){
		start=start-33;
		if(start==-165)
		{
			yd.style.top=0+"px";
			start=-33;
		}
	});
},2000);
};
//轮播图开始啦
var li=$(".box>li");
var count=0;
var time2=setInterval(function(){
	li.eq(count).stop().fadeOut(1000);
	if(count==2)
	{
		count=-1;
	}
	li.eq(count+1).stop().fadeIn(1000);
	count++;
	
},2000);
li.mouseover(function(){
	clearInterval(time2);
});
li.mouseout(function(){
	time2=setInterval(function(){
	li.eq(count).stop().fadeOut(1000);
	if(count==2)
	{
		count=-1;
	}
	li.eq(count+1).stop().fadeIn(1000);
	count++;
},2200);
});
//下拉菜单
var xiala=$(".banner .banz .right .xiala");
var bao=$(".banner .banz .right .bottom .bao");
xiala.click(function(){
	bao.css("overflow","inherit");
	$(this).hide();
});
var shangla=$(".banner .banz .right .bottom .bao .shangla .anniu");
shangla.click(function(){
	bao.css("overflow","hidden");
	xiala.show();
});
//轮播3
var start2=0;
var next2;
var u=$(".uu2");
var uul=$(".uu>li");
var time3=setInterval(function(){
	next2=start2-702;
	start2=next2;
	if(next2==-702)
	{
		uul.css("background-color","");
		uul.eq(1).css("backgroundColor","orange");
	}
	if(next2==-1404)
	{
		uul.css("backgroundColor","");
		uul.eq(2).css("backgroundColor","orange");
	}
	if(next2==-2106)
	{
		uul.css("backgroundColor","");
		uul.eq(3).css("backgroundColor","orange");
	}
	if(next2==-2808)
	{
		uul.css("backgroundColor","");
		uul.eq(0).css("backgroundColor","orange");
	}
	u.stop().animate({left:next2},1000,function(){
		if(next2==-2808)
		{
			start2=0;
			u.css("left",0);
		}
	});
},2000);
uul.mouseover(function(){
	clearInterval(time3);
});
uul.mouseout(function(){
	time3=setInterval(function(){
	next2=start2-702;
	start2=next2;
	if(next2==-702)
	{
		uul.css("background-color","");
		uul.eq(1).css("backgroundColor","orange");
	}
	if(next2==-1404)
	{
		uul.css("backgroundColor","");
		uul.eq(2).css("backgroundColor","orange");
	}
	if(next2==-2106)
	{
		uul.css("backgroundColor","");
		uul.eq(3).css("backgroundColor","orange");
	}
	if(next2==-2808)
	{
		uul.css("backgroundColor","");
		uul.eq(0).css("backgroundColor","orange");
	}
	u.stop().animate({left:next2},1000,function(){
		if(next2==-2808)
		{
			start2=0;
			u.css("left",0);
		}
	});
},2000);
});
u.mouseover(function(){
	clearInterval(time3);
});
u.mouseout(function(){
	time3=setInterval(function(){
	next2=start2-702;
	start2=next2;
	if(next2==-702)
	{
		uul.css("background-color","");
		uul.eq(1).css("backgroundColor","orange");
	}
	if(next2==-1404)
	{
		uul.css("backgroundColor","");
		uul.eq(2).css("backgroundColor","orange");
	}
	if(next2==-2106)
	{
		uul.css("backgroundColor","");
		uul.eq(3).css("backgroundColor","orange");
	}
	if(next2==-2808)
	{
		uul.css("backgroundColor","");
		uul.eq(0).css("backgroundColor","orange");
	}
	u.stop().animate({left:next2},1000,function(){
		if(next2==-2808)
		{
			start2=0;
			u.css("left",0);
		}
	});
},2000);
});
uul.click(function(){
	var index=$(this).index();
	uul.css("backgroundColor","");
	uul.eq(index).css("backgroundColor","orange");
//	console.log(index);
	next2=-702*index;
	start2=next2;
	u.stop().animate({left:next2},1000,function(){
		if(next2==-2808)
		{
			start2=0;
			u.css("left",0);
		}
	});
});
var start4=0;
var next4=0;
var box=$("body>.main3>.main3z>.right>.box").eq(0);
var pl=$("body>.main3>.main3z>.right>.box>.xia").eq(0);
var time4=setInterval(function(){
	next4=start4-183;
	start4=next4;
	pl.stop().animate({top:next4},1000,function(){
		if(next4==-732)
		{
			start4=0;
			pl.stop().css("top",0);
		}
	});
},2000);
box.mouseover(function(){
	clearInterval(time4);
});
box.mouseout(function(){
	time4=setInterval(function(){
	next4=start4-183;
	start4=next4;
	pl.stop().animate({top:next4},1000,function(){
		if(next4==-732)
		{
			start4=0;
			pl.stop().css("top",0);
		}
	});
},2000);
});


	var s1=$(".main9>.main9z>.top>span:nth-child(1)").eq(0);
	var s2=$(".main9>.main9z>.top>span:nth-child(2)").eq(0);
	var b1=$(".main9>.main9z>.bottom").eq(0);
	var b2=$(".main9>.main9z>.bottom2").eq(0);
	s1.mouseover(function(){
		s2.css("color","#000");
		s2.css("border-bottom","2px solid #fff");
		s1.css("color","#ed145b")
		s1.css("border-bottom","2px solid #ed145b");
		b2.hide();
		b1.show();
	});
	s2.mouseover(function(){
		s1.css("color","#000");
		s1.css("border-bottom","2px solid #fff");
		s2.css("color","#ed145b")
		s2.css("border-bottom","2px solid #ed145b");
		b1.hide();
		b2.show();
	});
	
	
	//新加功能
	
	
//	console.log(cart);
	var big=$(".adddiv_center");
	big.on("click","button",function(event){
		$.cookie.json = true;
		var cart =$.cookie("cart") || [];
		var src=event.target;
		var id=$(src).parent().data("id");
		var parent=$(src).parent();
//		console.log(src);
//		console.log($(src).parent().data("id"));
		var ele={
			"id":id,
			"img":parent.children("a").children("img").attr("src"),
			"title":parent.children("p").text(),
			"price":parent.children("div").text(),
			"amount":1
		}
//		console.log(ele);
		var has = cart.some(curr => {
					if (curr.id == id) { // 存在，则数量递增
						curr.amount++;
						return true
					}
					return false
				})
				if(!has)
					cart.push(ele);
//					console.log(ele.amount);
				// 将购物车保存到cookie中
				$.cookie("cart", cart, {expires: 7, path: "/"});
				
				var _end = $(".aside>li a").offset();
//				console.log(_end);
				var flyer = $(`<img src="${ele.img}" style="width:40px; height: 40px;">`);
//				console.log(flyer.fly);
//				console.log(_end);
//				console.log(event.pageX);
//				console.log($(window).scrollLeft());
				flyer.fly({
					start: {
						left:event.pageX - $(window).scrollLeft(),
						top: event.pageY - $(window).scrollTop()
					},
					end: {
						left: _end.left - $(window).scrollLeft(),
						top: _end.top - $(window).scrollTop()
					},
					onEnd: function() {
						this.destroy();
					}
				});
				
				
			
		
	
				
				
				
				
				
				
				
				
				
		
	});
	
	$.cookie.json = true;
		var cart = $.cookie("cart") || [];
			let len=cart.length;
			console.log(cart);
			console.log(len);
			$(".you>.circle").text(len);
	
	
	let welcome = $.cookie("welcome") || [];
			let login=$("a.login");
			let register=$("a.register");
			console.log(welcome);
			if(welcome.length==0)
			{
				
			}
			else{
				console.log(login.text());
				login.attr("href","/html/car.html");
				login.text("欢迎"+welcome[0]);
				register.text("退出");
				register.click(function(){
					$.cookie("welcome", [], {expires: 7, path: "/"});
				})
				register.attr("href","/index.html");
			}
	
	
}//load
